﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TianShiProject.EntityFrameworkCore;
using TianShiProject.Models.CarService.Tyres;
using TianShiProject.Models.Common;
using Volo.Abp.EntityFrameworkCore;

namespace TianShiProject.CarService.Tyres
{
    public class TyreRepository : DapperDbContextRepository, ITyreRepository
    {
        public TyreRepository(IDbContextProvider<TianShiProjectDbContext> dbContextProvider) : base(dbContextProvider)
        {

        }
        /// <summary>
        /// 显示轮胎使用记录
        /// </summary>
        /// <param name="cardnumber"></param>
        /// <param name="pin"></param>
        /// <param name="gui"></param>
        /// <param name="time"></param>
        /// <param name="pageindex"></param>
        /// <param name="pagesize"></param>
        /// <returns></returns>
        public async Task<PageResponse<TyreDto>> GetShowTyre(string cardnumber, string pin, string gui, DateTime? time, int pageindex, int pagesize)
        {
            PageResponse<TyreDto> response = new PageResponse<TyreDto>();
            var sql = $"select a.Id,a.TyreBrand,a.CarNumber,a.Specification,a.Num,a.UseTime,a.CreateTime,a.Remark,b.License,b.DriverName as UsePerson from Tyre a join Car b on a.CarNumber=b.CarNumber where a.IsDelete=0 ";
            if (!string.IsNullOrWhiteSpace(cardnumber))
            {
                sql += $" and a.CarNumber='{cardnumber}'";
            }
            if (!string.IsNullOrWhiteSpace(pin))
            {
                sql += $" and a.TyreBrand like '%" + pin + "%'";
            }
            if (!string.IsNullOrWhiteSpace(gui))
            {
                sql += $" and a.Specification like '%" + gui + "%'";
            }
            if (time != null)
            {
                sql += $" and a.UseTime='{time}'";
            }
            var list = await this.QueryAsync<TyreDto>(sql, null);
            response.PageTotal = list.Count;
            sql += $"order by a.Id offset {(pageindex - 1) * pagesize} rows fetch next {pagesize} rows only";
            var lists = await this.QueryAsync<TyreDto>(sql, null);
            response.PageData = lists;
            return response;
        }
        /// <summary>
        /// 删除轮胎使用记录
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<int> DeleteDelTyre(int id)
        {
            var sql = $"update Tyre set IsDelete=1 where Id={id}";
            return await ExecuteAsync(sql, null);
        }
        /// <summary>
        /// 批量删除轮胎使用记录
        /// </summary>
        /// <returns></returns>
        public async Task<int> DeleteDelsTyre(string ids)
        {
            var list = 0;
            var id = ids.Split(',');
            foreach (var item in id)
            {
                var sql = $"update Tyre set IsDelete=1 where Id={item}";
                list = await ExecuteAsync(sql, null);
            }
            return list;
        }
        /// <summary>
        /// 编辑修改
        /// </summary>
        /// <param name="tyreDto"></param>
        /// <returns></returns>
        public async Task<int> PutUpdateTyre(TyreDto tyreDto)
        {
            var sql = $"update Tyre set CarNumber='{tyreDto.CarNumber}',TyreBrand='{tyreDto.TyreBrand}',Specification='{tyreDto.Specification}',Num='{tyreDto.Num}',UseTime='{tyreDto.UseTime.ToString("yyyy/MM/dd")}',Remark='{tyreDto.Remark}' where Id='{tyreDto.Id}'";
            return await ExecuteAsync(sql, null);
        }
        /// <summary>
        /// 添加轮胎使用记录
        /// </summary>
        /// <param name="tyreDto"></param>
        /// <returns></returns>
        public async Task<int> PostAddTyre(TyreDto tyreDto)
        {
            var sql = $"insert into Tyre values('{tyreDto.TyreNumber}','{tyreDto.TyreBrand}','{tyreDto.CarNumber}','{tyreDto.Specification}','{tyreDto.Num}','','{tyreDto.UseTime.ToString("yyyy/MM/dd")}','{tyreDto.Remark}',GETDATE(),0)";
            var list = await ExecuteAsync
               (sql, tyreDto);
            return list;
        }
    }
}
